package org.jtb.flickrportlet;

import org.jtb.quickr.AuthResponse.Token;
import org.jtb.quickr.ContactsResponse.Contact;
import org.jtb.quickr.PeopleService;
import org.jtb.quickr.PhotoIdResponse;
import org.jtb.quickr.PhotosSearchCriteria;
import org.jtb.quickr.PhotosService;
import org.jtb.quickr.QuickrException;
import org.jtb.quickr.UserResponse;

public class PhotosHandler {
    private PhotosBean photosBean;
    private PageHandler pageHandler;
    private AuthHandler authHandler;
    private PrefsBean prefsBean;
    private ContactsBean contactsBean;
    
    private int page = 1;
    
    public void load(PhotosBean photosBean) throws QuickrException {
        PhotosSearchCriteria psc = new PhotosSearchCriteria();
        psc.setSort(PhotosSearchCriteria.Sort.DATE_POSTED_ASCENDING);
        psc.setPerPage(Integer.valueOf(prefsBean.getPerPage()));
        psc.setPage(page);
        
        if (getPageHandler().isMyPage()) {
            if (getAuthHandler().isAuthenticated()) {
                psc.setUserId(getAuthHandler().getUserId());
            } else {
                String userId = findByUserName(prefsBean.getUserName());
                psc.setUserId(userId);
            }
        } else if (getPageHandler().isContactsPage()) {
            Contact contact = contactsBean.getSelectedContact();
            if (contact == null) {
                throw new IllegalStateException("no selected contact");
            }
            String userId = contact.getNsId();
            psc.setUserId(userId);
        } else {
            throw new IllegalStateException("invalid page: " + pageHandler.getPage());
        }
        
        PhotosService ps = new PhotosService();
        photosBean.setPhotosResponse(ps.search(getAuthHandler().getToken(), psc));
    }
    
    public void delete(String id) throws QuickrException {
        if (!authHandler.isAuthenticated()) {
            throw new IllegalStateException("authentication required");
        }
        
        Token token = authHandler.getToken();
        PhotoIdResponse photoId = new PhotoIdResponse(id);
        PhotosService ps = new PhotosService();
        ps.delete(token, photoId);
    }
    
    private String findByUserName(String userName) throws QuickrException {
        PeopleService ps = new PeopleService();
        UserResponse ur = ps.findByUserName(userName);
        String userId = ur.getNsId();
        
        return userId;
    }
    
    public PhotosBean getPhotosBean() {
        return photosBean;
    }
    
    public void setPhotosBean(PhotosBean photosBean) throws QuickrException {
        this.photosBean = photosBean;
        
        if (prefsBean.getUserName() != null && prefsBean.getUserName().length() > 0) {
            if (!photosBean.isLoaded()) {
                load(photosBean);
            } else if (photosBean.getPerPage() != prefsBean.getPerPage()) {
                resetPage();
                load(photosBean);
            }
        }
    }
    
    public PageHandler getPageHandler() {
        return pageHandler;
    }
    
    public void setPageHandler(PageHandler pageHandler) {
        this.pageHandler = pageHandler;
    }
    
    public AuthHandler getAuthHandler() {
        return authHandler;
    }
    
    public void setAuthHandler(AuthHandler authHandler) {
        this.authHandler = authHandler;
    }
    
    public void setPrefsBean(PrefsBean prefsBean) {
        this.prefsBean = prefsBean;
    }
    
    public void resetPage() {
        page = 1;
    }
    
    public void setPage(int page) {
        this.page = page;
    }

    public void setContactsBean(ContactsBean contactsBean) {
        this.contactsBean = contactsBean;
    }
}
